package educrypt.gui.components;

import java.awt.Component;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;

import educrypt.gui.view.EducryptFileChooserView;
import educrypt.i18n.Messages;

class EducryptFileChooser extends JFileChooser implements
		EducryptFileChooserView {

	private static final long serialVersionUID = -4451710318047077022L;

	private FileNameExtensionFilter pdfFilter, docFilter, txtFilter, sqlFilter,
			csvFilter;

	private Component parent;

	public EducryptFileChooser(Component parent) {
		super();
		this.parent = parent;

	}

	public void setTextMode() {
		txtFilter = new FileNameExtensionFilter(
				Messages.getString("guiTxtFiles"), "txt");
		pdfFilter = new FileNameExtensionFilter(
				Messages.getString("guiPdfFiles"), "pdf");
		docFilter = new FileNameExtensionFilter(
				Messages.getString("guiDocFiles"), "doc");

		this.setFileFilter(pdfFilter);
		this.setFileFilter(docFilter);
		this.setFileFilter(txtFilter);
	}

	public void setTxtMode() {
		txtFilter = new FileNameExtensionFilter(
				Messages.getString("guiTxtFiles"), "txt");
		this.setFileFilter(txtFilter);
		this.setAcceptAllFileFilterUsed(false);
	}

	public void setCsvMode() {
		csvFilter = new FileNameExtensionFilter(
				Messages.getString("guiCsvFiles"), "csv");
		this.setFileFilter(csvFilter);
		this.setAcceptAllFileFilterUsed(false);
	}

	public void setSqlMode() {
		sqlFilter = new FileNameExtensionFilter(
				Messages.getString("guiSqlFiles"), "sql");
		this.setFileFilter(sqlFilter);
		this.setAcceptAllFileFilterUsed(false);
	}

	@Override
	public void approveSelection() {
		String fileName = this.getSelectedFile().getAbsolutePath();

		FileFilter selectedFileFilter = this.getFileFilter();

		fileName = addExtension(fileName, selectedFileFilter);

		File f = new File(fileName);

		if (f.exists() && this.getDialogType() == JFileChooser.SAVE_DIALOG) {
			int result = JOptionPane.showConfirmDialog(
					this.getTopLevelAncestor(),
					Messages.getString("guiOverwriteMessage"),
					Messages.getString("guiOverwriteTitle"),
					JOptionPane.YES_NO_CANCEL_OPTION,
					JOptionPane.QUESTION_MESSAGE);
			switch (result) {
			case JOptionPane.YES_OPTION:
				this.setSelectedFile(f);
				this.getTopLevelAncestor().setVisible(false);
				super.approveSelection();
				break;
			case JOptionPane.NO_OPTION:
				break;
			case JOptionPane.CANCEL_OPTION:
				this.cancelSelection();
				break;
			}
		} else {
			this.setSelectedFile(f);
			this.getTopLevelAncestor().setVisible(false);
			super.approveSelection();
		}
	}

	@Override
	public void cancelSelection() {
		this.getTopLevelAncestor().setVisible(false);
		super.cancelSelection();
	}

	private String addExtension(String fileName, FileFilter selectedFileFilter) {
		int lastIndexOfPoint = fileName.lastIndexOf(".");
		// > 0 because .name is valid and "name" is not an extension
		boolean havePossibleExtension = lastIndexOfPoint > 0;

		String possibleExtension = "";
		if (havePossibleExtension) {
			possibleExtension = fileName.substring(lastIndexOfPoint);
		}

		String completeFileName = fileName;
		if (selectedFileFilter.equals(txtFilter)
				&& !possibleExtension.equalsIgnoreCase(".txt")) {
			completeFileName = fileName + ".txt";
		}

		if (selectedFileFilter.equals(docFilter)
				&& !possibleExtension.equalsIgnoreCase(".doc")) {
			completeFileName = fileName + ".doc";
		}
		if (selectedFileFilter.equals(pdfFilter)
				&& !possibleExtension.equalsIgnoreCase(".pdf")) {
			completeFileName = fileName + ".pdf";
		}
		if (selectedFileFilter.equals(sqlFilter)
				&& !possibleExtension.equalsIgnoreCase(".sql")) {
			completeFileName = fileName + ".sql";
		}
		if (selectedFileFilter.equals(csvFilter)
				&& !possibleExtension.equalsIgnoreCase(".csv")) {
			completeFileName = fileName + ".csv";
		}
		return completeFileName;
	}

	@Override
	public void addListener(ActionListener ctr) {
		addActionListener(ctr);
	}

	@Override
	public void showSaveDialog() {
		updateUI();
		super.showSaveDialog(this.parent);

	}

	@Override
	public void showOpenDialog() {
		updateUI();
		super.showOpenDialog(this.parent);

	}
}
